const nodemailer = require('nodemailer');
require('dotenv').config();

// 创建邮件传输器
const transporter = nodemailer.createTransport({
    service: 'qq', // 使用QQ邮箱服务
    auth: {
        user: process.env.EMAIL_USER, // 发件人邮箱
        pass: process.env.EMAIL_PASS  // 邮箱授权码
    }
});

// 发送邮件的通用函数
async function sendMail(options) {
    const mailOptions = {
        from: `"漂流瓶" <${process.env.EMAIL_USER}>`,
        to: options.to,
        subject: options.subject,
        html: `
            <div style="padding: 20px; background-color: #f5f5f5;">
                <h2 style="color: #333;">${options.subject}</h2>
                <div style="background-color: white; padding: 20px; border-radius: 8px; margin-top: 20px;">
                    <p style="color: #666; line-height: 1.6;">${options.message}</p>
                    ${options.from ? `<p style="color: #999; margin-top: 20px;">来自: ${options.from}</p>` : ''}
                </div>
                <div style="color: #999; margin-top: 20px; font-size: 14px;">
                    这是一封来自漂流瓶系统的邮件
                </div>
            </div>
        `
    };

    try {
        const info = await transporter.sendMail(mailOptions);
        console.log('邮件发送成功:', info.messageId);
        return true;
    } catch (error) {
        console.error('邮件发送失败:', error);
        throw error;
    }
}

module.exports = { sendMail }; 